import { Controller, Post, Body, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiOkResponse, getSchemaPath, ApiBody } from '@nestjs/swagger';
import { FindManyOptions } from 'typeorm';
import { AuthGuard } from '@/common/guard/auth.guard';
import { SysService } from './sys.service';
import {
    CreateSysActionDto,
    CreateySysMenuDto,
    MoveSysMenuDto, GetActionsByMenuDto,
    CreateMenuActionDto
} from './dto/create-sy.dto';

import { UpdateSysMenuDto, UpdateSysActionDto } from './dto/update-sy.dto';
// import { LoginPipe } from '@/common/pipe/login.pipe';
import { DeleteDto, ResponseJson, SqlQueryDto } from '@/common/type/app.dto';
import { SqlQueryPipe } from '@/common/pipe/sqlquery.pipe';
import { SysAction } from './entities/sys.entity';

@Controller('sys')
@UseGuards(AuthGuard)
@ApiTags('系统维护')
// @ApiExtraModels(SysActionDto)
export class SysController {
    constructor(private readonly sysService: SysService) {}

       
    @Post('getmenutree')
    @ApiOperation({ summary: '取菜单tree'})
    @ApiOkResponse({
        schema: {
            allOf: [
                { $ref: getSchemaPath(ResponseJson) },
                {
                    properties: {
                        obj: {
                            type: 'array',
                            description: '菜单',
                            items: {
                                $ref: getSchemaPath(UpdateSysMenuDto)
                            }
                        }
                    }
                }
            ]
        },
    })
    getMenuTree() {
        return this.sysService.getMenuTree();
    }

    @Post('menucreate')
    @ApiOperation({ summary: '创建菜单'})
    @ApiOkResponse({
        schema: {
            allOf: [
                { $ref: getSchemaPath(ResponseJson) },
                {
                    properties: {
                        obj: {
                            type: 'object',
                            description: '菜单',
                            $ref: getSchemaPath(UpdateSysMenuDto)
                        }
                    }
                }
            ]
        },
    })
    createSysMenu(@Body() createySysMenu: CreateySysMenuDto) {
        return this.sysService.createSysMenu(createySysMenu);
    }

    @Post('menuupdate')
    @ApiOperation({summary: '修改菜单'})
    @ApiOkResponse({
        type: ResponseJson
    })
    updateSysMenu(@Body() updateSysMenuDto: UpdateSysMenuDto) {
        return this.sysService.updateSysMenu(updateSysMenuDto);
    }

    @Post('menudelete')
    @ApiOperation({ summary: '删除菜单'})
    @ApiOkResponse({
        type: ResponseJson
    })
    deleteSysMenu(@Body() deleteDto: DeleteDto) {
        return this.sysService.deleteSysMenu(deleteDto);
    }

    @Post('menumove')
    @ApiOperation({ summary: '移动菜单'})
    @ApiOkResponse({
        type: ResponseJson
    })
    moveSysMenu(@Body() moveDto: MoveSysMenuDto) {
        return this.sysService.moveSysMenu(moveDto);
    }

    @Post('actions')
    @ApiOperation({ summary: '取功能列表'})
    @ApiBody({ type: SqlQueryDto })
    @ApiOkResponse({
        schema: {
            allOf: [
                {$ref: getSchemaPath(ResponseJson)},
                {
                    properties: {
                        obj: {
                            type: 'array',
                            description: '功能数组',
                            items: {
                                $ref: getSchemaPath(UpdateSysActionDto)
                            }
                        }
                    },

                }
            ]
        }
    })
    getActions(@Body( SqlQueryPipe) query: FindManyOptions<SysAction>) {
        return this.sysService.getActions(query);
    }
    
    @Post('actioncreate')
    @ApiOperation({summary: '创建功能'})
    @ApiOkResponse({
        schema: {
            allOf: [
                {$ref: getSchemaPath(ResponseJson)},
                {
                    properties: {
                        obj: {
                            type: 'object',
                            description: '功能',
                            $ref: getSchemaPath(UpdateSysActionDto)
                        }
                    },

                }
            ]
        }
    })
    createAction(@Body() dto: CreateSysActionDto) {
        return this.sysService.createAction(dto);
    }

    @Post('actionupdate')
    @ApiOperation({ summary: '修改功能'})
    @ApiBody({type: UpdateSysActionDto})
    @ApiOkResponse({
        type: ResponseJson
    })
    updateAction(@Body() dto: UpdateSysActionDto) {
        return this.sysService.updateAction(dto);
    }

    @Post('actiondelete')
    @ApiOperation({ summary: '删除功能'})
    @ApiOkResponse({ type: ResponseJson})
    deleteAction(@Body() ids: DeleteDto) {
        return this.sysService.deleteAction(ids);
    }

    @Post('getmenuactionsbymenu')
    @ApiOperation({ summary: '取菜单关联的功能'})
    @ApiOkResponse({
        schema: {
            allOf: [
                {$ref: getSchemaPath(ResponseJson)},
                {
                    properties: {
                        obj: {
                            type: 'array',
                            description: '菜单功能数组',
                            items: {
                                type: 'object',
                                properties: {
                                    id: {
                                        type: 'number',
                                        description: '菜单功能ID'
                                    },
                                    remark: {
                                        type: 'string',
                                        description: '菜单功能备注'
                                    },
                                    action: {
                                        type: 'object',
                                        description: '功能',
                                        properties: {
                                            id: {
                                                type: 'number',
                                                description: '功能ID'
                                            },
                                            label: {
                                                type: 'string',
                                                description: '功能名称'
                                            },
                                            url: {
                                                type: 'string',
                                                description: '功能URL'
                                            },

                                        }
                                    }
                                }
                                
                            }
                        }
                    },

                }
            ]
        }
    })
    getActionsByMenu(@Body() dto: GetActionsByMenuDto) {
        return this.sysService.getActionsByMenu(dto);
    }

    @Post('getactionsfilternomenu')
    @ApiOperation({ summary: '取未关联菜单的功能数组'})
    @ApiOkResponse({
        schema: {
            allOf: [
                {$ref: getSchemaPath(ResponseJson)},
                {
                    properties: {
                        obj: {
                            type: 'array',
                            description: '功能数组',
                            items: {
                                $ref: getSchemaPath(UpdateSysActionDto)
                            }
                        }
                    },

                }
            ]
        }
    })
    getActionsFilterNoMenu(@Body() dto: GetActionsByMenuDto) {
        return this.sysService.getActionsByUnLinkMenu(dto);
    }

    @Post('createmenuactions')
    @ApiOperation({ summary: '创建菜单功能'})
    @ApiOkResponse({
        type: ResponseJson
    })
    createMenuActions(@Body() dto: CreateMenuActionDto[]) {
        return this.sysService.createMenuActions(dto);
    }

    @Post('deletemenuactions')
    @ApiOperation({ summary: '删除菜单功能'})
    @ApiOkResponse({ type: ResponseJson})
    deleteMenuActions(@Body() ids: DeleteDto) {
        return this.sysService.deleteMenuActions(ids);
    }

}
